// 云诺 https://app.witframe.com/market/list/index?uid=24
const Config = require('@/common/config/config')

const API = {}

API.getApiUrl = function() {
	return Config.apiurl;
}

API.request = function(url, method = "GET", data = {}) {
	return new Promise(function(resolve, reject) {
		url = Config.apiurl + url;
		if((JSON.stringify(data) == "{}")){
			data = {apikey: Config.apikey};
		}else{
			data = Object.assign(data, {apikey: Config.apikey});
		}
		
		uni.request({
			url: url,
			data: data,
			method: method,
			dataType:'json',
			header: {
				'Cache-Control': 'no-cache',
				'Content-Type': 'application/x-www-form-urlencoded',
			},
			success: function(res) {
				if (res.statusCode == 200) {
					if (res.data.errCode != 0 && res.data.message) {
						uni.showToast({
							title: res.data.message,
							icon: 'error',
							duration: 3000
						});
					}
					resolve(res.data);
				} else {
					uni.showToast({
						title: '请求数据出错',
						icon: 'loading',
						duration: 1000
					});
					reject(res.data);
				}
				
			},
			fail: function(err) {
				reject(err);
			}
		})
	});

}

API.get = function(url, data = {}) {
	return API.request(url, "GET", data);
}

API.post = function(url, data) {
	return API.request(url, "POST", data);
}


module.exports = API
